<?php

class Admin_Model_Estate extends Zend_Db_Table_Abstract
{
    protected $_name = 'estate';
    
    public function getAllData(){
		$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('e'=>$this->_name),array('e.estateId','e.estateCode','e.estateName','e.estateCode','e.rent','e.sale','e.otherInfo','e.baseUtility','e.otherUtility'));
        return $this->fetchAll($query);
    }
    public function getDataByTransaction($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('e'=>$this->_name),array('e.estateId','e.estateCode','e.estateName','e.estateCode','e.rent','e.sale','e.otherInfo','e.baseUtility','e.otherUtility'))
                        ->where("$data = 1");
        return $this->fetchAll($query);        
    }
    public function getDataByEstateId($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('e'=>$this->_name),array('e.estateId','e.estateCode','e.estateName','e.estateCode','e.rent','e.sale','e.otherInfo','e.baseUtility','e.otherUtility'))
                        ->where('estateId = ?',(int)$data);
        return $this->fetchRow($query);
    }
    public function getDataCheckUnique($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('e'=>$this->_name),array('e.estateId','e.estateCode','e.estateName','e.estateCode','e.rent','e.sale','e.otherInfo','e.baseUtility','e.otherUtility'));
        if(isset($data['estateName']) && $data['estateName']){
            $query->where('e.estateName = ?',$data['estateName']);
        }                                
        if(isset($data['estateCode']) && $data['estateCode']){
            $query->where('e.estateCode = ?',$data['estateCode']);
        }                        
        return $this->fetchRow($query);
    }
    public function addData($data){        
        $data = array('estateName'  => $data['estateName'],
                'estateCode'        => $data['estateCode'],
                'otherInfo'         =>$data['otherInfo'],
                'sale'              =>$data['sale'],
                'rent'              =>$data['rent'],
                'baseUtility'       =>$data['baseUtility'],
                'otherUtility'      =>$data['otherUtility']);
        $idAdd = $this->insert($data);        
        return $idAdd;
    }
    public function updateData($estateId = '',$data){        
        $data = array('estateName'  => $data['estateName'],
                'estateCode'        => $data['estateCode'],
                'otherInfo'         =>$data['otherInfo'],
                'sale'              =>$data['sale'],
                'rent'              =>$data['rent'],
                'baseUtility'       =>$data['baseUtility'],
                'otherUtility'      =>$data['otherUtility']);
        return $this->update($data, 'estateId = '.(int)$estateId);
    }
    
    public function getData($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.resourceId'))
                        ->join(array('ur'=>'user_role'),'r.roleId = ur.roleId', array('ur.userId'));
        if(isset($data['adminId']) && $data['adminId']){
            $query->where('ur.userId = ?',$data['adminId']);
        }
       
        if(isset($data['resourceId']) && $data['resourceId']){
            $data['resourceId'] = addslashes($data['resourceId']);
            $query->where('resourceId like (\'%"'.$data['resourceId'].'"%\')');
        }
        return $this->fetchAll($query);
    }    
    public function deleteData($roleId){
        $this->delete('estateId = '.(int)$roleId);
    }    
    public function getDataByRoleName($roleName){
        $query = $this->select()
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.resourceId'))
                        ->where('r.roleName = ?',$roleName);                        
    	return $this->fetchRow($query);
    }
    public function getDataByRoleId($roleid){  
        $query = $this->select()
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.resourceId'))
                        ->where('r.roleId = ?',(int)$roleid);
                                        
    	return $this->fetchRow($query);
    }
    public function getDataByUserId($userId){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.resourceId'))
                        ->join(array('ur'=>'user_role'),'r.roleId = ur.roleId', array('ur.userId'))
                        ->where('ur.userId = ?',(int)$userId);
                                        
    	return $this->fetchAll($query);
    }
   
}